package com.talkweb.newproject.admin.sysroleuser.service;

import com.talkweb.newproject.admin.sysresource.dto.SysResourceDTO;
import com.talkweb.newproject.admin.sysroleuser.dao.SysRoleUserDao;
import com.talkweb.newproject.admin.sysroleuser.dto.SysRoleUserDTO;
import com.talkweb.newproject.api.model.admin.SysRoleUserParam;
import com.talkweb.newproject.common.web.annotation.Page;
import org.apache.commons.collections.map.HashedMap;
import org.omg.PortableServer.ServantRetentionPolicy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qsf on 2019/5/9.
 */
@Service
public class SysRoleUserService implements Serializable {
    private static final long serialVersionUID=1L;
    @Autowired
    private SysRoleUserDao sysRoleUserDao;

    /**
     * 新增数据
     * @param sysRoleUserDTO
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = {Exception.class})
    public Long inserSysRoleUser(SysRoleUserDTO sysRoleUserDTO) throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("dto",sysRoleUserDTO);
        int count = sysRoleUserDao.insertSysRoleUser(params);
        SysRoleUserDTO dto = (SysRoleUserDTO) params.get("dto");
        return dto.getId();
    }

    /**
     * 更新数据
     * @param dto
     * @throws Exception
     */
    @Transactional(rollbackFor = {Exception.class})
    public void updateSysRoleUser(SysRoleUserDTO dto) throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("dto",dto);
        sysRoleUserDao.updateSysRoleUser(params);
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     * @throws Exception
     */
    public boolean deleteSysRoleUserById(String id) throws Exception {
        int count = sysRoleUserDao.deleteSysRoleUserById(id);
        if(count > 0) {
            return true;
        }else {
            return false;
        }
    }

    /**
     * 根据主键查询数据
     * @param id
     * @return
     * @throws Exception
     */
    public SysRoleUserDTO querySysRoleUserByPrimaryKey(String id) throws Exception {
        SysRoleUserDTO dto = sysRoleUserDao.findSysRoleUserByPrimaryKey(id);
        if(dto == null){
            dto=new SysRoleUserDTO();
        }
        return dto;
    }

    public SysRoleUserDTO queryLikeSysRoleUser(SysRoleUserDTO dto) throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("dto",dto);
        return sysRoleUserDao.queryLieSysRoleUser(params);
    }

    public List<SysRoleUserDTO> searchSysRoleUserByPaging(Map<String,Object> params) throws Exception {
        return sysRoleUserDao.searchSysRoleUserByPaging(params);
    }

    @Page
    public Object searchSysRoleUser(SysRoleUserParam dto) throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("dto",dto);
        List<SysRoleUserDTO> result = sysRoleUserDao.searchSysRoleUser(params);
        return result;
    }
}
